package com.activitycode.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activitycode.datamodel.friend.FansPet;
import com.activitycode.tools.OptionsTool;
import com.activitycode.tools.PublicTools;
import com.activitycode.util.CommConstant;
import com.activitycode.util.LogUtil;
import com.duostec.zhuyue.pets.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PetFriendAdapter extends BaseAdapter {

	public Context context;
	private List<FansPet> infoList;
	public PetFriendAdapter adapter;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions headOptions;
	private DisplayImageOptions optionsHead;

	public PetFriendAdapter(Context context, List<FansPet> data) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.infoList = data;

		initOptions();
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return infoList.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infoList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.adapter_frienddir, null);
			viewHolder = new ViewHolder();
			viewHolder.personalImageId = (ImageView) convertView
					.findViewById(R.id.personalImageId);
			viewHolder.name = (TextView) convertView
					.findViewById(R.id.item_name);
			viewHolder.sexId = (ImageView) convertView
					.findViewById(R.id.seximg);
			viewHolder.container = (LinearLayout) convertView
					.findViewById(R.id.pets);
			viewHolder.lineView = (ImageView) convertView
					.findViewById(R.id.line);
			viewHolder.distance = (TextView) convertView
					.findViewById(R.id.distance);
			// viewHolder.distance.setVisibility(View.GONE);
			convertView.setTag(viewHolder);
		} else {

			viewHolder = (ViewHolder) convertView.getTag();
		}

		imageLoader.displayImage(CommConstant.SERVICE_HOST_IP_LAN_IMG
				+ infoList.get(position).getHead_image(),
				viewHolder.personalImageId, optionsHead);
		viewHolder.name.setText(infoList.get(position).getNick_name());

		String distanceString = PublicTools.caculateDistance(context, infoList
				.get(position).getLatitude(), infoList.get(position)
				.getLogitude());
		Log.e(distanceString, distanceString);
		if (PublicTools.formatApprise(distanceString) < 1.0) {
			viewHolder.distance.setText(PublicTools.storeOnePosition(Double
					.parseDouble(distanceString) * 1000) + "m");
		} else if (PublicTools.formatApprise(distanceString)>10000.0) {
			viewHolder.distance.setText(R.string.unkown_distance);
		}else{
			viewHolder.distance.setText(PublicTools.storeOnePosition(Double
					.parseDouble(distanceString)) + "km");
		}

		// Log.e("name", infoList.get(position).getNick_name());

		try {
			if (infoList.get(position).getSex().equals("0")) {

				viewHolder.sexId.setBackground(context.getResources()
						.getDrawable(R.drawable.male2));
			} else {
				viewHolder.sexId.setBackground(context.getResources()
						.getDrawable(R.drawable.female));
			}
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}

		try {

			viewHolder.container.removeAllViews();

			View pet = null;

			if (infoList.get(position).getPetList() != null)
			// || infoList.get(position).getPetList().size() != 0
			// | infoList.get(position).getPetList().toString()!= "false")
			{
				Log.e("petListSize", ""
						+ infoList.get(position).getPetList().size());
				for (int i = 0; i < infoList.get(position).getPetList().size(); i++) {
					Log.e("exec3", "Y");
					pet = LayoutInflater.from(context).inflate(R.layout.ui_pet,
							null);
					ImageView petView = (ImageView) pet.findViewById(R.id.pet);
					if (infoList.get(position).getPetList().get(i)
							.getHead_image() == null
							|| "".equals(infoList.get(position).getPetList()
									.get(i).getHead_image())) {
						imageLoader.displayImage(
								CommConstant.SERVICE_HOST_IP_LAN_IMG
										+ infoList.get(position).getPetList()
												.get(i).getPet_head_image(),
								petView, headOptions);
						viewHolder.container.addView(pet);
					} else {
						imageLoader.displayImage(
								CommConstant.SERVICE_HOST_IP_LAN_IMG
										+ infoList.get(position).getPetList()
												.get(i).getHead_image(),
								petView, headOptions);
						viewHolder.container.addView(pet);
					}

				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	public final static class ViewHolder {
		private ImageView personalImageId;
		private ImageView sexId;
		private TextView name;
		private ImageView lineView;
		private TextView distance;
		private LinearLayout container;

	}

	private void initOptions() {
		headOptions = OptionsTool.getHeadOptions1(context);
		optionsHead = OptionsTool.getHeadOptions(context);
	}

}
